package com.devnn.base;

import java.util.Timer;
import java.util.TimerTask;

import com.devnn.baseNet.BaseDAO;
import com.devnn.view.InputDialog;
import com.devnn.view.LoadDialog;
import com.devnn.view.ConfirmDialog;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;

/**
 * @author devnn
 */
public abstract class BaseActivity extends FragmentActivity implements BaseDAO.RequestListener, OnClickListener {
    protected String TAG = this.getClass().getSimpleName();
    private LoadDialog loadDialog = null;
    private ConfirmDialog confirmDialog;
    private InputDialog inputDialog;

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        Log.d(TAG, "onCreate()");
        super.onCreate(arg0);
        init();
        setData();
    }

    /**
     * 在xml文件的返回按钮中加上属性android:onClick="back",不需要再给它加单击事件
     */
    public void back(View view) {
        finish();
    }

    /**
     * 初始化控件
     */
    protected abstract void init();

    /**
     * 给控件设置属性
     */
    protected abstract void setData();


    /**
     * 显示加载框
     */
    public void showLoadDialog() {
        if (loadDialog == null) {
            loadDialog = new LoadDialog(this);
        }
        loadDialog.show();
    }

    /**
     * 显示请求对话框
     *
     * @param message
     */
    public void showLoadDialog(String message) {
        if (loadDialog == null) {
            loadDialog = new LoadDialog(this);
        }
        loadDialog.show();
        loadDialog.setMessage(message);
    }

    /**
     * 显示请求对话框
     *
     * @param message
     */
    public void showLoadDialog(String message, boolean cancelable) {
        if (loadDialog == null) {
            loadDialog = new LoadDialog(this);
        }
        loadDialog.show();
        loadDialog.setMessage(message);
        loadDialog.setCancelable(cancelable);
    }


    public void showInfoDialog(String message) {
        if (confirmDialog == null) {
            confirmDialog = new ConfirmDialog(this);
        }
        confirmDialog.show();
        confirmDialog.setCancelVisiable(View.GONE);
        confirmDialog.setOnSelectedListener(null);
        confirmDialog.setMessage(message);
    }

    /**
     * 显示输入框
     *
     * @param message
     */
    public void showInputDialog(String message, int inputType, InputDialog.OnInputSureListener onInputSureListener) {
        if (inputDialog == null) {
            inputDialog = new InputDialog(this);
        }
        inputDialog.show();
        inputDialog.setOnInputSureListener(onInputSureListener);
        inputDialog.setMessage(message);
        inputDialog.setInputType(inputType);
    }

    public void showConfirmDialog(String message, ConfirmDialog.OnSelectedListener onConfirmListener) {
        if (confirmDialog == null) {
            confirmDialog = new ConfirmDialog(this);
        }
        confirmDialog.show();
        confirmDialog.setCancelVisiable(View.VISIBLE);
        confirmDialog.setOnSelectedListener(onConfirmListener);
        confirmDialog.setMessage(message);
    }

    /**
     * 取消加载框
     */
    public void closeLoadDialog() {
        if (loadDialog != null) {
            loadDialog.cancel();
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        Log.d(TAG, "onDestroy()");
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        Log.d(TAG, "onPause()");
        super.onPause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        Log.d(TAG, "onResume()");
        super.onResume();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        Log.d(TAG, "onStop()");
        super.onStop();
    }


    @Override
    public void onRequestSucceed(BaseDAO baseDAO) {
        closeLoadDialog();
    }

    @Override
    public void onRequestFailed(BaseDAO baseDAO) {
        closeLoadDialog();
        if (baseDAO.getErrorCode() == BaseDAO.ERROR_CODE_NONET) {
            showInfoDialog("请检查网络");
        } else {
            showInfoDialog(baseDAO.getErrorMessage());
        }
    }

    /**
     * 打开软键盘
     *
     * @param editView
     */
    protected void showInputWindow(final View editView) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            public void run() {
                InputMethodManager inputManager = (InputMethodManager) BaseActivity.this.getSystemService(
                        Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(editView, 0);
            }
        }, 500);
    }

    /**
     * 关闭软键盘
     */
    protected void closeInputWindow() {
        if (null != this.getCurrentFocus() && null != this.getCurrentFocus().getWindowToken()) {
            InputMethodManager imm = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
